package cn.hu.basic.controller;

import cn.hu.basic.commonenum.ErrorCode;
import cn.hu.basic.domain.dto.SmsRegisterDto;
import cn.hu.basic.service.IVerifyCodeService;
import cn.hu.basic.util.AjaxResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;

@RestController
@RequestMapping("/verify_code")
public class VerifyCodeController {
    @Autowired
    private IVerifyCodeService verifyCodeService;

    @GetMapping("/image/{imageCodeKey}")
    public AjaxResult imageVerifyCode(@PathVariable(value = "imageCodeKey") String imageCodeKey){
       return verifyCodeService.imageVerifyCode(imageCodeKey);
    }
    /**
     * 短信验证码发送
     * @return
     */
    @PostMapping("/smsCode")
    public AjaxResult sendSmsCode(@RequestBody SmsRegisterDto dto)  {
        try {
            return verifyCodeService.sendSmsCode(dto);
        }catch (IOException e){
            e.printStackTrace();
            return AjaxResult
                    .getInstance()
                    .setCode(ErrorCode.SYSTEM_ERROR.getCode())
                    .setSuccess(false)
                    .setMessage(ErrorCode.SYSTEM_ERROR.getMessage());
        }

    }

}
